<template>
  <!-- 权限管理 -->
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <!-- 点击上面的添加权限,  添加是一级, 是一级访问权 -->
          <el-button type="primary" size="small" @click="add(1,'0')">添加权限</el-button>
        </div>
        <el-table border :data="permissionList" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <!-- 点击下面的添加权限点, 添加的是二级, 是二级操作权(这里row.id 作为将来的pid) -->
              <el-button v-if="row.type===1" size="small" type="text" @click="add(2,row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="checkPermission(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="delPermission(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 新增编辑弹层 -->
      <el-dialog title="showTitle" :visible="showPermissionDialog" @close="handleClose">
        <!-- 表单内容 -->
        <el-form label-width="100px">
          <el-form-item label="权限名称">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <!-- 开关的背景色 -->
            <el-switch
              v-model="formData.enVisible"
              active-text="启用"
              active-value="1"
              inactive-text="不启用"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <div style="text-align: right;">
            <el-button @click="handleClose">取消</el-button>
            <el-button type="primary" @click="clickSubmit">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
// 引入获取权限管理文件,新增,删除,更新,查看详情
import { reqGetPermissionList, reqAddPermission, reqDelPermission, reqUpdatePermission, reqGetPermissionDetail } from '@/api/permission'
// 导入将列表数据转换为树型数据文件
import { transListTree } from '@/utils/index'
export default {
  name: 'Permission',
  data() {
    return {
      permissionList: [],
      showPermissionDialog: false,
      formData: {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      }
    }
  },
  // 计算属性控制标题
  computed: {
    showTitle() {
      return this.formData.id ? '添加权限' : '查看编辑'
    }

  },
  created() {
    this.getPermissionList()
  },
  methods: {

    async getPermissionList() {
      const { data } = await reqGetPermissionList()
      this.permissionList = transListTree(data, '0')
    },
    // 新增权限
    add(type, id) {
      // console.log(type, id)
      this.formData.type = type
      this.formData.pid = id
      this.showPermissionDialog = true
    },
    handleClose() {
      this.showPermissionDialog = false
      this.formData = {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
    },
    // 新增
    async addPermission() {
      await reqAddPermission(this.formData)
      this.handleClose()
      this.getPermissionList()
      this.$message.success('新增权限成功')
    },
    // 删除
    async delPermission(id) {
      const res = await this.$confirm('确定要删除该权限', '温馨提示').catch(error => (error))
      if (res === 'cancel') return
      await reqDelPermission(id)
      this.$message.success('删除权限成功')
      this.getPermissionList()
    },
    // 查看时显示数据
    async checkPermission(id) {
      const { data } = await reqGetPermissionDetail(id)
      this.formData = data
      this.showPermissionDialog = true
    },
    // 更新数据或新增
    async clickSubmit() {
      if (this.formData.id) {
        await reqUpdatePermission(this.formData)
        this.$message.success('更新权限成功')
      } else {
        await reqAddPermission(this.formData)
        this.$message.success('添加权限成功')
      }
      this.handleClose()
      this.getPermissionList()
    }

  }

}
</script>

<style>

</style>
